﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using AppLayer;
using SpotItComponents;
using SpotItServerAPI;
using AudioPlayerLib;
using WaveResources;
using Vitruvian.Distribution;
using Vitruvian.Services;
using Vitruvian.Communications.Sockets;
using System.Threading;

namespace SpotIt
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        AudioFiles theAudioFiles = AudioFiles.Instance;
        SpotIt.Properties.Settings settings = new Properties.Settings();
        public MainWindow()
        {
            InitializeComponent();
            theAudioFiles.addFile("bubble_bobble_-02-_ecran_titre.mp3", true);
            theAudioFiles.addFile("LearnIt.mp3", true);
            theAudioFiles.addFile("buttonBeep1.mp3", false);
            theAudioFiles.addFile("buttonBeep2.mp3", false);
            theAudioFiles.addFile("bubble_bobble_-04-_ingame_music.mp3", true);
            //
            //apply volume settings
            theAudioFiles.theFiles[0].changeVolume(settings.musicVolume);
            theAudioFiles.theFiles[1].changeVolume(settings.musicVolume);
            theAudioFiles.theFiles[2].changeVolume(settings.buttonBeepVolume);
            theAudioFiles.theFiles[3].changeVolume(settings.buttonBeepVolume);
            theAudioFiles.theFiles[4].changeVolume(settings.musicVolume);
            //
            //
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {

        }

        private void usernameTextBox_GotFocus(object sender, RoutedEventArgs e)
        {

        }

        private void usernameTextBox_LostFocus(object sender, RoutedEventArgs e)
        {

        }

        private void logonButton_Click(object sender, RoutedEventArgs e)
        {
            DistributionService distributionService = ServiceRegistry.GetPreferredService<DistributionService>();

            string remoteEPstr = "";
            if (ipTextBox.Text.CompareTo("") != 0 && portTextBox.Text.CompareTo("") != 0)
                remoteEPstr = ipTextBox.Text + ":" + portTextBox.Text;
            if (string.IsNullOrEmpty(remoteEPstr))
                remoteEPstr = "127.0.0.1:2345";

            // Parse the user specific ip address and port
            IPEndPointFormatter ipEndPointFormatter = new IPEndPointFormatter();
            IPEndPoint2 remoteEP = ipEndPointFormatter.Unformat(remoteEPstr) as IPEndPoint2;

            // Create a communication provider (TCP Client) to connect with the server
            TcpClientProvider provider = new TcpClientProvider();
            provider.RemoteEndPoint = remoteEP;

            distributionService.AddProvider(provider);

            Services services = new Services();
            services.WaitForAllDistributedServices();

            // register the player

            //OperationResult result = services.accountManager.LoginPlayer(usernameTextBox.Text);
            OperationResult result = services.accountManager.Login("admin", "admin");
            Session session = new Session();
            if (result.Success)
                session = result.Data as Session;
            else
            {
                MessageBox.Show("Login Error! Be sure to enter a username.");
            }

            // Generate test data
            //TestDataGenerator testDataGenerator = new TestDataGenerator(services);
            //testDataGenerator.GenerateData();

            if (result.Success)
            {
                SpotIt_MainWindow main = new SpotIt_MainWindow(session, services);
                main.Show();
                theAudioFiles.theFiles[2].Play();
                this.Close();
            }
        }

        #region IP and Port text box behavior

        private void ipTextBox_GotFocus(object sender, RoutedEventArgs e)
        {
            if (ipTextBox.Foreground != Brushes.Black)
            {
                ipTextBox.Foreground = Brushes.Black;
                ipTextBox.Text = "";
            }
        }

        private void ipTextBox_LostFocus(object sender, RoutedEventArgs e)
        {
            if (ipTextBox.Text.CompareTo("") == 0)
            {
                ipTextBox.Foreground = Brushes.DarkGray;
                ipTextBox.Text = "127.0.0.1";
            }
        }

        private void portTextBox_GotFocus(object sender, RoutedEventArgs e)
        {
            if (portTextBox.Foreground != Brushes.Black)
            {
                portTextBox.Foreground = Brushes.Black;
                portTextBox.Text = "";
            }
        }

        private void portTextBox_LostFocus(object sender, RoutedEventArgs e)
        {
            if (portTextBox.Text.CompareTo("") == 0)
            {
                portTextBox.Foreground = Brushes.DarkGray;
                portTextBox.Text = "2345";
            }
        }

        #endregion

        private void customServer_Collapsed(object sender, RoutedEventArgs e)
        {
            //logonButton.Margin = new Thickness(31, 79, 142, 68);
        }

        private void customServer_Expanded(object sender, RoutedEventArgs e)
        {
            //logonButton.Margin = new Thickness(31, 79, 142, 68);
            //logonButton.Margin = new Thickness(31, 39, 142, 108);
        }

        private void logOn_keyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Return)
            {
                DistributionService distributionService = ServiceRegistry.GetPreferredService<DistributionService>();

                string remoteEPstr = "";
                if (ipTextBox.Text.CompareTo("") != 0 && portTextBox.Text.CompareTo("") != 0)
                    remoteEPstr = ipTextBox.Text + ":" + portTextBox.Text;
                if (string.IsNullOrEmpty(remoteEPstr))
                    remoteEPstr = "127.0.0.1:2345";

                // Parse the user specific ip address and port
                IPEndPointFormatter ipEndPointFormatter = new IPEndPointFormatter();
                IPEndPoint2 remoteEP = ipEndPointFormatter.Unformat(remoteEPstr) as IPEndPoint2;

                // Create a communication provider (TCP Client) to connect with the server
                TcpClientProvider provider = new TcpClientProvider();
                provider.RemoteEndPoint = remoteEP;

                distributionService.AddProvider(provider);

                Services services = new Services();
                services.WaitForAllDistributedServices();

                // register the player

                //OperationResult result = services.accountManager.LoginPlayer(usernameTextBox.Text);
                OperationResult result = services.accountManager.Login("admin", "admin");
                Session session = new Session();
                if (result.Success)
                    session = result.Data as Session;
                else
                {
                    MessageBox.Show("Login Error! Be sure to enter a username.");
                }

                // Generate test data
                //TestDataGenerator testDataGenerator = new TestDataGenerator(services);
                //testDataGenerator.GenerateData();

                if (result.Success)
                {
                    SpotIt_MainWindow main = new SpotIt_MainWindow(session, services);
                    main.Show();
                    //theAudioFiles.theFiles[2].Play();
                    this.Close();
                }
            }
        }

        private void passwordTextBox_GotFocus(object sender, RoutedEventArgs e)
        {
            if (passwordTextBox.Foreground != Brushes.Black)
            {
                passwordTextBox.Foreground = Brushes.Black;
                passwordTextBox.Text = "";
            }
        }

        private void passwordTextBox_LostFocus(object sender, RoutedEventArgs e)
        {
            if (passwordTextBox.Text == "")
            {
                passwordTextBox.Foreground = Brushes.DarkGray;
                passwordTextBox.Text = "admin";
            }
        }

        private void usernameTextBox_LostFocus_1(object sender, RoutedEventArgs e)
        {
            if (usernameTextBox.Text == "")
            {
                usernameTextBox.Foreground = Brushes.DarkGray;
                usernameTextBox.Text = "admin";
            }
        }

        private void usernameTextBox_GotFocus_1(object sender, RoutedEventArgs e)
        {
            if (usernameTextBox.Foreground != Brushes.Black)
            {
                usernameTextBox.Foreground = Brushes.Black;
                usernameTextBox.Text = "";
            }
        }



    }
}
